package ch.common.service.umTableService.impl;

import java.util.ArrayList;
import java.util.List;


import ch.common.dao.BaseDao;
import ch.common.service.umTableService.UMTableService;
import ch.common.util.jsonValueProcessor.JsonDateTimeValueProcessor;
import ch.common.util.page.PageModel;
import ch.yiyangerp.model.SysUser;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 
 * @author grc
 * @param <T>
 * @Date 2016-1-5 13:20:40
 */
public class UMTableServiceImpl<T,T_DAO extends BaseDao<T>> implements UMTableService<T, T_DAO> {

	// Spring注入
	private T_DAO t_Dao ;
	// Spring注入
	private T t;
	
	@SuppressWarnings("unchecked")
	@Override
	public String doQuery(int pageNumber,int pageSize) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		// TODO Auto-generated method stub
		String msg = null;
		String from = "from " + t.getClass().getSimpleName() + " ";
		String where = doSetWhere(t);
		
		String hql = from + where;
		
		List<T> list = t_Dao.findByPage(hql, pageNumber, pageSize);
		PageModel<T> pageModel = new PageModel<T>();
		pageModel.setList(list);
		pageModel.setPageNo(pageNumber);
		pageModel.setPageSize(pageSize);
		pageModel.setTotalRecords((int)t_Dao.findCount((Class<T>)t.getClass(),where));
		
		msg = doChangeToJsonObject(pageModel).toString();
		
		return msg;
	}

	@Override
	public void doDelete(T t) {
		// TODO Auto-generated method stub
		t_Dao.delete(t);
	}
	
	@Override
	public void doSave(T t) {
		// TODO Auto-generated method stub
		t_Dao.save(t);
	}
	
	@SuppressWarnings("unchecked")
	public int doGetPageCounts(int pageSize) {
		return ((int)t_Dao.findCount((Class<T>) t.getClass())/pageSize)+1;
	};
	
	/**
	 * 将 泛型类t 转成json字符串 并返回。
	 */
	protected static JSONObject doChangeToJsonObject(Object o){

		JSONObject jsonObject = null;
		JsonConfig jsonConfig = new JsonConfig();
		/*
		 * 将时间类型都转成'YYYY-MM-DD hh:mm:ss'格式字符串。
		 */
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonDateTimeValueProcessor());	
		jsonObject = JSONObject.fromObject(o,jsonConfig);//java对象转化为JSON对象
		System.out.println(jsonObject.toString());
		
		return jsonObject;
	}

	/**
	 * 
	 * 根据类具有的属性通过反射生成hql的where部分字符串。
	 * @return where 字符串
	 * @param o
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	 */
	protected String doSetWhere(Object o) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		
		StringBuffer where = new StringBuffer(" where 1=1 ");

		Class<?> clazz = o.getClass(); // 获取对象o的Class对象
		Field[] fields = clazz.getDeclaredFields(); // 获取对象o的所有属性
		for(Field f : fields)
		{	
			if(f.getType().equals(String.class)) // 如果类型为 String
			{
				StringBuffer methodName = new StringBuffer("get")
						.append(f.getName().substring(0, 1).toUpperCase()) // 属性名第一个字母大写
						.append(f.getName().substring(1));
			
				@SuppressWarnings("unchecked")
				Method getter = clazz.getMethod(methodName.toString());// 获取对应getter的方法
				
				
				String s = (String) getter.invoke(o);
				if(s != null && !s.equals(""))
				{
					where.append(" and " + f.getName() + " like '%" + s + "%'");
				}
			}
		}
		
		return where.toString();
	}
	
	public T_DAO getT_Dao() {
		return t_Dao;
	}

	public T getT() {
		return t;
	}

	public void setT(T t) {
		this.t = t;
	}

	public void setT_Dao(T_DAO t_Dao) {
		this.t_Dao = t_Dao;
	}
}
